package com.ruoyi.warehouse.service.impl;

import java.util.List;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import com.ruoyi.warehouse.domain.CpkcFnishedProductInventory;
import com.ruoyi.warehouse.domain.LlslIncomingMaterials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.warehouse.domain.FhslQuantityShipped;
import com.ruoyi.warehouse.mapper.FhjlShippingRecordFormMapper;
import com.ruoyi.warehouse.domain.FhjlShippingRecordForm;
import com.ruoyi.warehouse.service.IFhjlShippingRecordFormService;

import javax.annotation.Resource;

/**
 * 成品发货Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class FhjlShippingRecordFormServiceImpl implements IFhjlShippingRecordFormService 
{
    @Autowired
    private FhjlShippingRecordFormMapper fhjlShippingRecordFormMapper;
    @Resource
    private CodeClient codeClient;

    /**
     * 查询成品发货
     * 
     * @param fhCode 成品发货主键
     * @return 成品发货
     */
    @Override
    public FhjlShippingRecordForm selectCpkcFnishedProductInventoryCpCode(String fhCode)
    {
        return fhjlShippingRecordFormMapper.selectCpkcFnishedProductInventoryCpCode(fhCode);
    }

    /**
     * 查询成品发货
     *
     * @param cpCode 成品发货主键
     * @return 成品发货
     */
    public FhjlShippingRecordForm selectTj(String cpCode){
        return fhjlShippingRecordFormMapper.selectTj(cpCode);
    }

    /**
     * 查询成品发货列表
     * 
     * @param fhjlShippingRecordForm 成品发货
     * @return 成品发货
     */
    @Override
    public List<FhjlShippingRecordForm> selectFhjlShippingRecordFormList(FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        return fhjlShippingRecordFormMapper.selectFhjlShippingRecordFormList(fhjlShippingRecordForm);
    }

    /**
     * 新增成品发货
     * 
     * @param fhjlShippingRecordForm 成品发货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFhjlShippingRecordForm(FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        fhjlShippingRecordForm.setFhCode(codeClient.getCode(26));
        fhjlShippingRecordForm.setFhPeo(SecurityUtils.getUsername());

        int rows = fhjlShippingRecordFormMapper.insertFhjlShippingRecordForm(fhjlShippingRecordForm);
        insertFhslQuantityShipped(fhjlShippingRecordForm);
        return rows;
    }

    /**
     * 修改成品发货
     * 
     * @param fhjlShippingRecordForm 成品发货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFhjlShippingRecordForm(FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        fhjlShippingRecordFormMapper.deleteFhslQuantityShippedByFhCode(fhjlShippingRecordForm.getFhCode());
        insertFhslQuantityShipped(fhjlShippingRecordForm);
        return fhjlShippingRecordFormMapper.updateFhjlShippingRecordForm(fhjlShippingRecordForm);
    }

    /**
     * 批量删除成品发货
     * 
     * @param fhCodes 需要删除的成品发货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFhjlShippingRecordFormByFhCodes(String[] fhCodes)
    {
        return fhjlShippingRecordFormMapper.deleteFhjlShippingRecordFormByFhCodes(fhCodes);
    }

    /**
     * 删除成品发货信息
     * 
     * @param fhCode 成品发货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFhjlShippingRecordFormByFhCode(String fhCode)
    {
        return fhjlShippingRecordFormMapper.deleteFhjlShippingRecordFormByFhCode(fhCode);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param fhjlShippingRecordForm 成品发货对象
     */
    @Transactional
    public void insertFhslQuantityShipped(FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        List<CpkcFnishedProductInventory> fhslQuantityShippedList = fhjlShippingRecordForm.getCpkcList();
        String fhCode = fhjlShippingRecordForm.getFhCode();
        String cpCode = fhjlShippingRecordForm.getCpCode();
        if (StringUtils.isNotNull(fhslQuantityShippedList))
        {
            List<FhslQuantityShipped> list = new ArrayList<FhslQuantityShipped>();
            for (CpkcFnishedProductInventory cpkc: fhslQuantityShippedList)
            {
                cpkc.getFhslQuantityShipped().setFhCode(fhCode);
                cpkc.getFhslQuantityShipped().setCpCode(cpCode);
                list.add(cpkc.getFhslQuantityShipped());
            }
            if (list.size() > 0)
            {
//                System.out.println(list.get(0));
                //修改库存中的预出库数
//                fhjlShippingRecordFormMapper.updateCpkcBeforehandNum(list);
                //添加出库详情
                fhjlShippingRecordFormMapper.batchFhslQuantityShipped(list);
            }
        }
    }

    /**
     * 产品发货审批
     * */
    @Transactional
    public int examine(String fhCode){
        //根据发货编码查询发货产品信息
        List<FhslQuantityShipped> fhslQuantityShippeds = fhjlShippingRecordFormMapper.selectFhslByFhCode(fhCode);

        // 获取来料单中的来料详情
        if (StringUtils.isNotNull(fhslQuantityShippeds))
        {
            for (FhslQuantityShipped fhsl : fhslQuantityShippeds)
            {
                // 补全来料详情信息并判断是否审核成功
                int i = fhjlShippingRecordFormMapper.examineDeliveryStorage(fhsl);
                // 审核失败报错，并回滚
                if(i==0){
                    throw new RuntimeException("库存不足，出库失败");
                }
            }
        }
        // 修改状态
        return fhjlShippingRecordFormMapper.updateCpkcIsExamine(fhCode);
    }


    /**
     * 产品发货撤销
     * */
    @Transactional
    public int cancel(String fhCode){

        //根据发货编码查询发货产品信息
        List<FhslQuantityShipped> fhslQuantityShippeds = fhjlShippingRecordFormMapper.selectFhslByFhCode(fhCode);
        // 获取来料单中的来料详情
        if (StringUtils.isNotNull(fhslQuantityShippeds))
        {
            for (FhslQuantityShipped fhsl : fhslQuantityShippeds)
            {
                // 补全来料详情信息并判断是否撤销成功
                int i = fhjlShippingRecordFormMapper.cancelDeliveryStorage(fhsl);
                // 不成功报错
                if(i==0){
                    throw new RuntimeException("撤销失败");
                }
            }
        }
        // 修改状态
        return fhjlShippingRecordFormMapper.updateCpkcIsExamineCancel(fhCode);
    }
}
